//
//  WSMBaseTabBarController.m
//  WinesMall
//
//  Created by Isaac on 17/8/29.
//  Copyright © 2017年 Isaac. All rights reserved.
//

#import "WSMBaseTabBarController.h"
#import "WSMBaseNavigationController.h"
#import "WSMBaseViewController.h"

#define kClassKey   @"rootVCClassString"
#define kTitleKey   @"title"
#define kImgKey     @"imageName"
#define kSelImgKey  @"selectedImageName"

@interface WSMBaseTabBarController ()<UITabBarControllerDelegate>

@end

@implementation WSMBaseTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, UI_tabBar_Height)];
    backView.backgroundColor = kMainColor;
    [self.tabBar insertSubview:backView atIndex:0];
    self.tabBar.opaque = YES;
    self.delegate = self;
    
    NSArray *childItemsArray = @[
                                 
                                 @{kClassKey  : @"ISAACHomeViewController",
                                   kTitleKey  : @"首页",
                                   kImgKey    : @"homeNoSelect",
                                   kSelImgKey : @"index-selected-index"},
                                 
                                 @{kClassKey  : @"ISAACFindViewController",
                                   kTitleKey  : @"发现",
                                   kImgKey    : @"findNoSelect",
                                   kSelImgKey : @"findSelect"},
                                 
                                 @{kClassKey  : @"ISAACColumnViewController",
                                   kTitleKey  : @"专栏号",
                                   kImgKey    : @"专栏未选中",
                                   kSelImgKey : @"zl-selected-zl"},
                                 
                                 @{kClassKey  : @"ISAACNewsFlashController",
                                   kTitleKey  : @"快讯",
                                   kImgKey    : @"newFlashNoSelect",
                                   kSelImgKey : @"kx-selected-kx"},
                                 
                                 @{kClassKey  : @"ISAACMineViewController",
                                   kTitleKey  : @"我的",
                                   kImgKey    : @"我的未选中",
                                   kSelImgKey : @"我的选中"}];
    
    [childItemsArray enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL *stop) {
        WSMBaseViewController *vc = [NSClassFromString(dict[kClassKey]) new];
        vc.title = dict[kTitleKey];
        WSMBaseNavigationController *nav = [[WSMBaseNavigationController alloc] initWithRootViewController:vc];
        UITabBarItem *item = nav.tabBarItem;
        //        item.badgeValue = @"1";
        item.title = dict[kTitleKey];
        item.image = [UIImage imageNamed:dict[kImgKey]];
        item.selectedImage = [[UIImage imageNamed:dict[kSelImgKey]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        [item setTitleTextAttributes:@{NSForegroundColorAttributeName : kPriceColor} forState:UIControlStateSelected];
        
        [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:kWhiteColor,NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
        
        [self addChildViewController:nav];
    }];

    
}


#pragma mark 监听tabbar事件点击
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    if (viewController == [tabBarController.viewControllers objectAtIndex:3] && [userToken isEqualToString:@""])     {
        
//        [[WSMTools wsmTool] pointToLoginPage];
//        return NO;
        return YES;
    }
    else {
        return YES;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
